package com.bcx.crm.spring_boot.springMVC.fileUpload;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by ys on 2018/4/22.
 * 1.通过@RequestParam注解获取指定(files)值
 */
@Controller
public class UploadController {
    @PostMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("files") MultipartFile[] files){  //1.
        try {
            for (MultipartFile file:files){
                FileUtils.writeByteArrayToFile(new File("d:/upload/"+file.getOriginalFilename()),file.getBytes());
            }
            return "ok";
        } catch (IOException e) {
            e.printStackTrace();
            return "wrong";
        }
    }
}
